package com.utils;

import android.content.Context;
import android.content.SharedPreferences;
import com.baselearn.app.entity.User;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;

/**
 * Created by happy_000 on 2014/11/27.
 */
public class UserUtil {
    private static String SAVENAME="spname";
    private static int pageSize;
    private static int pageIndex;

    public  static User getUser(Context context){
        SharedPreferences sharedPreferences=context.getSharedPreferences(SAVENAME,Context.MODE_PRIVATE);
        User user=new User();
        user.setName(sharedPreferences.getString(User.NAME,User.EMPTY));
        user.setPasswd(sharedPreferences.getString(User.NAME, User.EMPTY));
        user.setSavePasswd(sharedPreferences.getBoolean(User.ISSAVEPASSWD, false));
        return user;
    }
    public  static User getUser(Context context,String DBName){
        User user=new User();
        DbUtils dbUtils=DbUtils.create(context,DBName);
        try {
         //   dbUtils.findAll(User.class);
//            List<User> list = dbUtils.findAll(Selector.from(User.class)
//                    .where("id" ,"<", 54)
//                    .and(WhereBuilder.b("age", ">", 20).or("age", " < ", 30))
//                    .orderBy("id")
//                    .limit(pageSize)
//                    .offset(pageSize * pageIndex));
            // op为"in"时，最后一个参数必须是数组或Iterable的实现类(例如List等)
        // user = dbUtils.findFirst(Selector.from(User.class).where("id", "in", new int[]{1, 2, 3}));
            // op为"between"时，最后一个参数必须是数组或Iterable的实现类(例如List等)
            // user = dbUtils.findFirst(Selector.from(User.class).where("id", "between", new String[]{"1", "5"}));
            user=dbUtils.findFirst(User.class);

        } catch (DbException e) {
            e.printStackTrace();
        }
        return user;
    }
    public  static void saveOrUpdateUser(Context context,User user){
        SharedPreferences sharedPreferences=context.getSharedPreferences(SAVENAME,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString(User.NAME,user.getName());
        editor.putString(User.PASSWD,user.getPasswd());
        editor.putBoolean(User.ISSAVEPASSWD,user.isSavePasswd());
        editor.commit();
    }
    public  static void saveOrUpdateUser(Context context,User user,String DBName){
        DbUtils dbUtils=DbUtils.create(context,DBName);
        try {
            dbUtils.saveOrUpdate(user);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    public  static void removeUser(Context context,User user){
        SharedPreferences sharedPreferences=context.getSharedPreferences(SAVENAME,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.clear();
        editor.apply();
    }
    public  static void removeUser(Context context,User user,String DBName){
        DbUtils dbUtils=DbUtils.create(context, DBName);
        try {
            dbUtils.delete(user);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
}
